package org.boyan.modules.controller;

import org.boyan.cases.ExcelToCaseBody;
import org.boyan.common.api.vo.Result;
import org.boyan.common.util.DateUtils;
import org.boyan.entity.cases.Case;
import org.boyan.entity.esb.ESBServer;
import org.boyan.entity.trans.Trans;
import org.boyan.esb.ExcelToESB;
import org.boyan.esb.GenerateEsbExcel;
import org.boyan.modules.service.CaseService;
import org.boyan.modules.service.ESBService;
import org.boyan.modules.service.TransService;
import org.boyan.utils.ExcelExportUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/esb")
public class ESBController {
    @Value("${boyan.path.upload}")
    private String uploadPath;

    @Autowired
    private TransService transService;

    @Autowired
    private CaseService caseService;

    @Autowired
    private ESBService esbService;

    //导出ESB接口的方法
    @PostMapping("/exportESB")
    public Result exportESB(@RequestBody List<Case> caseList){
        String targetPath="";
        for(Case caseInfo:caseList){
            String filePath=uploadPath+"/excel/"+caseInfo.getCaseNo()+".xlsx";
            File file=new File(filePath);
            if(!file.exists()){
                return Result.error("文件丢失请重新上传!");
            }
            //根据已有文件生成ESB文件的方法
            List<Map<String,Object>> bodyList=ExcelToCaseBody.getCaseBody(file);
            String targetFileName="";
            for(Map<String,Object> bodyMap:bodyList){
                Trans trans= (Trans) bodyMap.get("trans");
                targetFileName=targetFileName+trans.getTransCode()+"-";
            }
            targetPath=uploadPath+"/ESBExport/"+ DateUtils.formatDate(new Date(),"yyyyMMdd")+"/"+targetFileName+"ESB接口文档.xlsx";
            File targeFile=new File(targetPath);
            //ESB导出接口文件文件夹不存在则创建文件夹
            if(!targeFile.getParentFile().exists()){
                targeFile.getParentFile().mkdirs();
            }
            //将module复制到该文件出
            try {
                ExcelExportUtils.copyModuleFile(uploadPath+"/modules/ESB接口导入模板.xlsx",targetPath);
                GenerateEsbExcel.generateEsbExcel(file,targetPath);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("文件发生意外错误!");
            }
            //修改交易的信息
            for(Map<String,Object> bodyMap:bodyList){
                Trans trans= (Trans) bodyMap.get("trans");
                transService.changeTransStatus(2,trans.getTransCode());
            }
        }
        return Result.OK(targetPath);
    }

    @PostMapping("/uploadESB")
    @Transactional
    public Result uploadESB(@RequestParam("file") MultipartFile multipartFile, @RequestParam("id") String id){
        List<ESBServer> esbServerList= ExcelToESB.getESBServer(multipartFile);
        List<Trans> transList=transService.getTransListByCaseId(id);
        //如果两者size不同表示肯定有问题
        if(esbServerList.size()!= transList.size()){
            return Result.error("ESB接口文件解析出错，请确认是否正确上传文件？");
        }
        List<String> transCodeList=new ArrayList<>(transList.size());
        for(Trans trans:transList){
            transCodeList.add(trans.getTransCode());
        }
        for(ESBServer esbServer:esbServerList){
            if(!transCodeList.contains(esbServer.getTransCode())){
                return Result.error("ESB接口文件解析出错，请确认是否正确上传文件？");
            }
        }
        //此处为交易添加esb信息
        String filename="";
        for(ESBServer esbServer:esbServerList){
            if(!transService.updateESB(esbServer)){
                return Result.error("ESB导入失败");
            }
            //添加ESBServer服务相关信息
            esbService.addESB(esbServer);
            filename=esbServer.getTransCode()+",";
        }
        filename=filename.substring(0,filename.length()-1);
        //此处文件保存成功，将文件存档
        File file=new File(uploadPath+"/ESBreturn/服务定义接口-"+filename+".xlsx");
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.OK("ESB接口导入失败");
        }
        //修改case状态
        caseService.changeCaseStatus(4,id);
        return Result.OK("ESB接口导入成功");
    }
}
